//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "DPlusDMinus.h"
using namespace Cephei::QL::Methods::Finitedifferences;
#include <gen/QL/Math/Array.h>
#include <gen/QL/Methods/Finitedifferences/TridiagonalOperator.h>
using namespace Cephei::QL::Math;
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Methods::Finitedifferences::CDPlusDMinus::CDPlusDMinus (UInt64 gridPoints, Double h) : CTridiagonalOperator(CDPlusDMinus::typeid)
{
    try
    {
#ifdef HANDLE
        _phDPlusDMinus = NULL;
#endif
        QuantLib::Size _gridPoints = (QuantLib::Size)ValueHelper::Convert (gridPoints); //d
        QuantLib::Real _h = (QuantLib::Real)ValueHelper::Convert (h); //d
        _ppDPlusDMinus = new boost::shared_ptr<QuantLib::DPlusDMinus> (new QuantLib::DPlusDMinus ( _gridPoints,  _h ));
        SetTridiagonalOperator (boost::dynamic_pointer_cast<QuantLib::TridiagonalOperator> (*_ppDPlusDMinus));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Methods::Finitedifferences::CDPlusDMinus::CDPlusDMinus (boost::shared_ptr<QuantLib::DPlusDMinus>& childNative, Object^ owner) : CTridiagonalOperator(CDPlusDMinus::typeid)
{
#ifdef HANDLE
	_phDPlusDMinus = NULL;
#endif
	_ppDPlusDMinus = &childNative;
    _ppTridiagonalOperator = new boost::shared_ptr<QuantLib::TridiagonalOperator> (boost::dynamic_pointer_cast<QuantLib::TridiagonalOperator> (*_ppDPlusDMinus));
}
Cephei::QL::Methods::Finitedifferences::CDPlusDMinus::CDPlusDMinus (QuantLib::DPlusDMinus& childNative, Object^ owner) : CTridiagonalOperator(CDPlusDMinus::typeid)
{
#ifdef HANDLE
	_phDPlusDMinus = NULL;
#endif
	_ppDPlusDMinus = new boost::shared_ptr<QuantLib::DPlusDMinus> (&childNative);
    _ppTridiagonalOperator = new boost::shared_ptr<QuantLib::TridiagonalOperator> (boost::dynamic_pointer_cast<QuantLib::TridiagonalOperator> (*_ppDPlusDMinus));
    _DPlusDMinusOwner = owner;
    _TridiagonalOperatorOwner = owner;
}

Cephei::QL::Methods::Finitedifferences::CDPlusDMinus::CDPlusDMinus (CDPlusDMinus^ copy) : CTridiagonalOperator(CDPlusDMinus::typeid)
{
#ifdef HANDLE
	_phDPlusDMinus = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppDPlusDMinus = new boost::shared_ptr<QuantLib::DPlusDMinus> (copy->GetShared());
        _ppTridiagonalOperator = new boost::shared_ptr<QuantLib::TridiagonalOperator> (boost::dynamic_pointer_cast<QuantLib::TridiagonalOperator> (*_ppDPlusDMinus));
    }
}
Cephei::QL::Methods::Finitedifferences::CDPlusDMinus::CDPlusDMinus (PLATFORM::Type^ t) : CTridiagonalOperator(CDPlusDMinus::typeid)
{
#ifdef HANDLE
	_phDPlusDMinus = NULL;
#endif
	if (!t->IsSubclassOf(CDPlusDMinus::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Methods::Finitedifferences::CDPlusDMinus::CDPlusDMinus (QuantLib::Handle<QuantLib::DPlusDMinus>& childNative, Object^ owner)  : CTridiagonalOperator(CDPlusDMinus::typeid)
{
	_phDPlusDMinus = &childNative;
	_ppDPlusDMinus = &static_cast<boost::shared_ptr<QuantLib::DPlusDMinus>>(childNative.currentLink());
    _ppTridiagonalOperator = new boost::shared_ptr<QuantLib::TridiagonalOperator> (boost::dynamic_pointer_cast<QuantLib::TridiagonalOperator> (*_ppDPlusDMinus));
    _DPlusDMinusOwner = owner;
}
Cephei::QL::Methods::Finitedifferences::CDPlusDMinus::CDPlusDMinus (QuantLib::Handle<QuantLib::DPlusDMinus> childNative)  : CTridiagonalOperator(CDPlusDMinus::typeid)
{
	_phDPlusDMinus = &childNative;
	_ppDPlusDMinus = &static_cast<boost::shared_ptr<QuantLib::DPlusDMinus>>(childNative.currentLink());
    _ppTridiagonalOperator = new boost::shared_ptr<QuantLib::TridiagonalOperator> (boost::dynamic_pointer_cast<QuantLib::TridiagonalOperator> (*_ppDPlusDMinus));
}
#endif
#ifdef STRUCT
Cephei::QL::Methods::Finitedifferences::CDPlusDMinus::CDPlusDMinus (QuantLib::DPlusDMinus childNative)  : CTridiagonalOperator(CDPlusDMinus::typeid)
{
#ifdef HANDLE
	_phDPlusDMinus = NULL;
#endif
	_ppDPlusDMinus = new boost::shared_ptr<QuantLib::DPlusDMinus> (new QuantLib::DPlusDMinus (childNative));
    _ppTridiagonalOperator = new boost::shared_ptr<QuantLib::TridiagonalOperator> (boost::dynamic_pointer_cast<QuantLib::TridiagonalOperator> (*_ppDPlusDMinus));
}
#endif

Cephei::QL::Methods::Finitedifferences::CDPlusDMinus::~CDPlusDMinus ()
{
    if (_ppDPlusDMinus != NULL)
    {
	    delete _ppDPlusDMinus;
        _ppDPlusDMinus = NULL;
    }
}
Cephei::QL::Methods::Finitedifferences::CDPlusDMinus::!CDPlusDMinus ()
{
    if (_ppDPlusDMinus != NULL)
    {
	    delete _ppDPlusDMinus;
    }
}
QuantLib::DPlusDMinus& Cephei::QL::Methods::Finitedifferences::CDPlusDMinus::GetReference ()
{
    if (_ppDPlusDMinus == NULL) throw REFNEW NativeNullException ();
	return **_ppDPlusDMinus;
}
boost::shared_ptr<QuantLib::DPlusDMinus>& Cephei::QL::Methods::Finitedifferences::CDPlusDMinus::GetShared ()
{
    if (_ppDPlusDMinus == NULL) throw REFNEW NativeNullException ();
	return *_ppDPlusDMinus;
}
QuantLib::DPlusDMinus* Cephei::QL::Methods::Finitedifferences::CDPlusDMinus::GetPointer ()
{
    if (_ppDPlusDMinus == NULL) throw REFNEW NativeNullException ();
	return &**_ppDPlusDMinus;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::DPlusDMinus>& Cephei::QL::Methods::Finitedifferences::CDPlusDMinus::GetHandle ()
{
	if (_phDPlusDMinus == NULL)
	{
		_phDPlusDMinus = new Handle<QuantLib::DPlusDMinus> (*_ppDPlusDMinus);
	}
	return *_phDPlusDMinus;
}
#endif
bool Cephei::QL::Methods::Finitedifferences::CDPlusDMinus::HasNative () 
{
	return (_ppDPlusDMinus != NULL);
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Methods::Finitedifferences::IDPlusDMinus^ Cephei::QL::Methods::Finitedifferences::CDPlusDMinus_Factory::Create (UInt64 gridPoints, Double h)
{
    return REFNEW CDPlusDMinus ( gridPoints,  h);
}
